package com.viphuli.app.tool.fragment;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.DatePicker;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.bean.part.ArrangeType;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.fragment.NumberPickerDialogFragment.NumberClickListener;
import com.viphuli.app.tool.http.ApiRequest;
import com.viphuli.app.tool.utils.OtherUtils;

/**
 * 添加加班
 * 
 * @author li.li
 *
 */
public class HolidayOverWorkAddFragment extends BaseProgressFragment {

	private AccountUser toUser;//当前选择的用户

	private int year;
	private int monthOfYear;
	private int dayOfMonth;
	private int number;
	private ArrangeType arrangeType;

	@Bind(R.id.id_holiday_user_name)
	protected TextView toUserName;

	@Bind(R.id.id_holiday_data_text)
	protected TextView dateText;

	@Bind(R.id.id_holiday_number_text)
	protected TextView numberText;

	@Bind(R.id.id_holiday_type_text)
	protected TextView typeText;

	@Bind(R.id.id_holiday_remark)
	protected TextView remarkText;

	private DatePickerDialog.OnDateSetListener DatePickerListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

			String date = year + "." + (monthOfYear + 1) + "." + dayOfMonth;
			dateText.setText(date);

			HolidayOverWorkAddFragment.this.year = year;
			HolidayOverWorkAddFragment.this.monthOfYear = monthOfYear;
			HolidayOverWorkAddFragment.this.dayOfMonth = dayOfMonth;

		}
	};

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_holiday_over_work_add;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getActivity().getMenuInflater().inflate(R.menu.menu_holiday_over_work_add, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());
		if (tokenKeeper.isLogin()) {

			if (toUser == null) {
				ViewUtils.toast("请选择成员");
				return false;
			}

			if (arrangeType == null) {
				ViewUtils.toast("请选择类型");
				return false;
			}

			if (year == 0 || monthOfYear == 0 || dayOfMonth == 0) {
				ViewUtils.toast("请选择日期");
				return false;
			}

			if (number == 0) {
				ViewUtils.toast("请选择时长");
				return false;
			}

			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, monthOfYear);
			cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);

			long start = OtherUtils.maxLength(cal.getTimeInMillis());

			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getUser().getUid());
			params.put("to_uid", toUser.getUid());
			params.put("arrange_type_id", arrangeType.getId());
			params.put("start", start);
			params.put("hour", number);

			if (StringUtils.isNotBlank(remarkText.getText().toString())) {
				params.put("remark", remarkText.getText().toString());
			}

			ApiRequest.holidayAdd.request(this, params);
			return true;

		} else {
			ViewUtils.toast("请登陆后重试");
		}

		return false;
	}

	public void setToUser(AccountUser toUser) {
		this.toUser = toUser;

		toUserName.setText(toUser.getUserName());

	}

	@OnClick(R.id.id_holiday_user)
	public void clickUser() {

		//成员列表
		CircleMemberDialogFragment dialogFrag = new CircleMemberDialogFragment();
		dialogFrag.show(getChildFragmentManager(), dialogFrag.getClass().getSimpleName());

	}

	@OnClick(R.id.id_holiday_data)
	public void clickDate() {

		Calendar objTime = Calendar.getInstance();
		int iYear = objTime.get(Calendar.YEAR);
		int iMonth = objTime.get(Calendar.MONTH);
		int iDay = objTime.get(Calendar.DAY_OF_MONTH);

		DatePickerDialog dialog = new DatePickerDialog(getActivity(), DatePickerListener, iYear, iMonth, iDay);
		dialog.show();

	}

	@OnClick(R.id.id_holiday_number)
	public void clickNumber() {

		NumberPickerDialogFragment dialogFrag = new NumberPickerDialogFragment(new NumberClickListener() {

			@Override
			public void onEnsure(int number) {

				if (number > 0) {
					numberText.setText(number + "小时");
					HolidayOverWorkAddFragment.this.number = number;
				}
			}
		}, 0, 8,"小时");
		dialogFrag.show(getChildFragmentManager(), dialogFrag.getClass().getSimpleName());

	}

	@OnClick(R.id.id_holiday_type)
	public void clickType() {

		//加班类型列表
		HolidayOverWorkTypeDialogFragment dialogFrag = new HolidayOverWorkTypeDialogFragment();
		dialogFrag.show(getChildFragmentManager(), dialogFrag.getClass().getSimpleName());

	}

	public void setArrangeType(ArrangeType arrangeType) {
		this.arrangeType = arrangeType;

		typeText.setText(arrangeType.getName());
	}

	public static void go(Activity act) {

		Bundle args = new Bundle();
		MyPageHelper.holidayWorkRecordFragment.showMyPage(act, args);

	}

}
